<?php

namespace app\admin\controller;
use think\Db;

/**
 * @Class Turnover
 * @User zhang
 */
class Turnover extends \think\Controller
{
    /**
     * @description 个人流水查询
     * @param $user_id 用户id
     */
    public function index()
    {
        $user_id = input('id');
        $turnover=Db::field('user.user_name,tu.turnover,tu.type,tu.time,tu.id')
            ->table('zx_user user,zx_turnover tu')
            ->where("tu.user_id=$user_id and user.id=tu.user_id")->paginate(20);
        $page = $turnover->render();
        $this->assign('page', $page);
//        dump( $user_id);exit;
        $this->assign('user_id', $user_id );
        $this->assign('turnover', $turnover);

////        $this->assign('pid', $pid);
        return $this->fetch('index');
    }

//    public function shoppingcar_del_a()
//    {
//        $shoppingcar_id = input('shoppingcar_id');
//        $rus=db('shoppingcar')->where('id',$shoppingcar_id)->delete();
//        if($rus){
//            $this->success('删除成功');
//        }else{
//            $this->error('删除失败');
//        }
//
//    }

    /**
     * @return mixed
     *@description 资金转移页面
     */
    public function remove(){
        $user_id = input('id');
        $rus=db('user')->field('balance')->where("id=$user_id")->find();
//        dump($rus);exit;
        $this->assign('user_id', $user_id );
        $this->assign('rus',$rus);
        return $this->fetch('add');
    }

    /**
     * @description 资金转移
     * @param $name 表单提交的金额
     */
    public function turnover_add_bt(){
//        dump(input());
        $money=is_numeric(input('name'));
        if(!$money){
            $this->error('请输入正确格式');
        }
        if(input('name')<=0){
            $this->error('请输入正确金额');
        }
        $rus=db('user')->field('balance')->where("id",input('user_id'))->find();
        if($rus['balance']<input('name')){
            $this->error('余额不足');
        }else{
            $balance=$rus['balance']-input('name');
            $data=['balance'=> $balance];
            $rus=db('user')->where("id",input('user_id'))->update($data);
            if($rus){
                $this->success('转移成功',"admin/user/user_detail?user_id=".input('user_id'));
            }else{
                $this->error('发生错误');
            }
        }
    }
}